package com.iseastar.guojiang.newcabinet;

import android.os.Bundle;
import android.os.Message;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.iseastar.dianxiaosan.model.Const;
import com.iseastar.guojiang.BaseFragment;
import com.iseastar.guojiang.app.Intents;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.dialog.DialogMgr;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.model.ServiceTaskBean;
import com.iseastar.guojiang.model.TaskDetailBackBean;
import com.iseastar.guojiang.newcabinet.adapter.TaskOtherParcelAdapter;
import com.shouhuobao.station.R;

import java.text.MessageFormat;

import droid.frame.utils.lang.Str;
import droid.frame.utils.sqlite.DateUtils;

/**
 * Created by joker on 2017/12/19.
 * 任务详情
 */
public class TaskDetailFragment extends BaseFragment implements View.OnClickListener {

    private TextView mTaskTypeTV;//任务详情类型
    private TextView mSendOrderTimeTV;//派单时间
    private TextView mTaskDetailDescTV;//任务描述
    private TextView mAddressNameTV;//地址名字
    private TextView mAddressDetailsTV;//详细地址
    /**
     * 联系人的布局
     */
    private LinearLayout mContractLayoutLL;
    private TextView mContractNameTV;//寄件人名字
    private TextView mTaskCodeTV;//任务编号
    private TextView mParcelNumTV;//包裹数量

    private ListView mListView;
    private TaskOtherParcelAdapter mAdapter = null;

    private ServiceTaskBean serviceTaskBean = null;
    private String parcelNumStr = "<font color=#4A4A4A>{0}个包裹</font>";
    private ImageView mContractStationTV;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (null != bundle) {
            serviceTaskBean = (ServiceTaskBean) bundle.getSerializable("item");
            showLoadingDialog(null);
            AppHttp.getInstance().getCourierTaskDetailData(serviceTaskBean.getTaskId());
        }
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.fragment_task_detail);
        super.findViewById();
        mTaskTypeTV = (TextView) findViewById(R.id.task_type_tv);
        mSendOrderTimeTV = (TextView) findViewById(R.id.send_order_time_tv);
        mTaskDetailDescTV = (TextView) findViewById(R.id.task_desc_tv);
        mAddressNameTV = (TextView) findViewById(R.id.address_name_tv);
        mAddressDetailsTV = (TextView) findViewById(R.id.address_details_tv);
        mContractLayoutLL = (LinearLayout) findViewById(R.id.contract_layout_ll);
        mContractNameTV = (TextView) findViewById(R.id.contract_name_tv);
        findViewById(R.id.contract_phone_tv).setOnClickListener(this);
        mContractStationTV = (ImageView) findViewById(R.id.contract_station_tv);
        mContractStationTV.setOnClickListener(this);
        mTaskCodeTV = (TextView) findViewById(R.id.task_code_tv);
        mParcelNumTV = (TextView) findViewById(R.id.parcel_num_tv);
        mListView = (ListView) findViewById(R.id.listview);
        mAdapter = new TaskOtherParcelAdapter(null, getActivityContext(), mListView);
        mListView.setAdapter(mAdapter);
        updateView();
    }

    private void updateView() {
        if (null == serviceTaskBean) {
            return;
        }
        if (Const.CourierTaskStatus.transferTask == serviceTaskBean.getTaskType()) {//回仓
            ((ImageView) findViewById(R.id.contract_phone_tv)).setImageResource(R.drawable.task_phone_icon);
            mContractLayoutLL.setVisibility(View.VISIBLE);
            mTaskCodeTV.setVisibility(View.GONE);
            mTaskTypeTV.setText("回仓交接");
            mTaskTypeTV.setBackgroundResource(R.drawable.parcel_to_step_5c7cd8_bg);
            mSendOrderTimeTV.setText("派单时间：" + DateUtils.format(serviceTaskBean.getTaskPostTime()));
            mAddressNameTV.setText(serviceTaskBean.getWareHouseName());
            mAddressDetailsTV.setText(serviceTaskBean.getAddress());
            mContractNameTV.setText("联系人：" + serviceTaskBean.getContactName());
            mTaskDetailDescTV.setText("￥" + serviceTaskBean.getTaskLucre() + "/" + Str.formatDoubleData(serviceTaskBean.getDistance() / 1000) + "km");
        } else {
            ((ImageView) findViewById(R.id.contract_phone_tv)).setImageResource(R.drawable.task_recivier_phone_icon);
            mTaskCodeTV.setVisibility(View.VISIBLE);
            mContractLayoutLL.setVisibility(View.VISIBLE);
            mTaskTypeTV.setBackgroundResource(R.drawable.parcel_to_step_bg);
            mSendOrderTimeTV.setText("派单时间：" + DateUtils.format(serviceTaskBean.getTaskPostTime()));
            mAddressNameTV.setText(serviceTaskBean.getBuildName());
            mAddressDetailsTV.setText(serviceTaskBean.getAddress());
            mTaskCodeTV.setText("任务编号：" + serviceTaskBean.getTaskNo());
            mTaskDetailDescTV.setText("￥" + serviceTaskBean.getTaskLucre() + "/" + Str.formatDoubleData(serviceTaskBean.getDistance() / 1000) + "km");
            if (Const.CourierTaskStatus.stationTask == serviceTaskBean.getTaskType()) {//驿站
                mTaskTypeTV.setText("驿站揽收");
                mContractNameTV.setText("商户：" + serviceTaskBean.getContactName());
            } else if (Const.CourierTaskStatus.cabinetTask == serviceTaskBean.getTaskType()) {//快递柜
                mContractLayoutLL.setVisibility(View.GONE);
                mTaskTypeTV.setText("快递柜取件");

            } else {//上门
                mTaskTypeTV.setText("上门取件");
                mContractNameTV.setText("寄件人：" + serviceTaskBean.getContactName());
                //上门是否显示驿站标识
                if (Str.isNotEmpty(serviceTaskBean.getStationName())) {
                    mContractStationTV.setVisibility(View.VISIBLE);
                } else {
                    mContractStationTV.setVisibility(View.GONE);
                }
            }
        }
    }

    /**
     * 刷新布局的所有数据
     *
     * @param bean
     */
    public void refreshDataToView(ServiceTaskBean bean) {
        this.serviceTaskBean = bean;
        showLoadingDialog(null);
        AppHttp.getInstance().getCourierTaskDetailData(serviceTaskBean.getTaskId());
        updateView();
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            //包裹列表返回数据
            case MsgID.courier_get_task_detail_data:
                cancelLoadingDialog();
                final ReqResult<TaskDetailBackBean> result = JSON.parser(msg.obj, TaskDetailBackBean.class);
                if (checkLoginStatus(result)) {
                    getActivityContext().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            TaskDetailBackBean backBean = result.getResult();
                            if (null != backBean) {
                                ServiceTaskBean bean = backBean.getTask();
                                if (bean != null) {

                                }
                                mAdapter.setItems(backBean.getDetails(), true);
                                mParcelNumTV.setText(Html.fromHtml(MessageFormat.format(parcelNumStr, mAdapter.getItemsSize())));
                            }
                        }
                    });
                } else {
                    showToast(result.getMessage());
                }
                return true;
            default:
                return super.handleMessage(msg);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //拨打联系人电话
            case R.id.contract_phone_tv:
                if (null == serviceTaskBean) {
                    return;
                }
                if (serviceTaskBean.getTaskType() == Const.CourierTaskStatus.visitTask) {
                    DialogMgr.receiverOrSendCall(getActivityContext(), serviceTaskBean.getPhone(), 3, serviceTaskBean.getAddress(), null);
                } else {
                    Intents.dialOrCall(serviceTaskBean.getPhone());
                }
                break;
            //获取驿站详情
            case R.id.contract_station_tv:
                DialogMgr.stationShow(getActivityContext(), serviceTaskBean);
                break;
        }
    }

}
